/*******************************************************************************
 *
 *    Copyright (c) Dolphin Browser
 *
 *    ViewTest
 *
 *    MotionDetector
 *    TODO File description or class description.
 *
 *    @author: derron
 *    @since:  2010-3-10
 *    @version: 1.0
 *
 ******************************************************************************/
package com.sdk.widget.threescreen.scrollview;

import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;

/**
 * MotionDetector of ViewTest.
 * @author derron
 *
 */
public class MotionDetector {
    private static final int MAX_MOVEMENT_IN_CLICKING = DisplayManager.dipToPixel(5);
    private static final int FLING_GESTURE_THREHOLD = 250;
    private static final double MOTION_INTENT_FACTOR = 0.5;
    private MotionEvent mLatestEvent;
    private MotionEvent mStartPoint;

    private VelocityTracker mVelocityTracker;

    public MotionDetector(){
        super();
    }

    public static enum MotionIntent {
        UNKNOWN,VIRTICAL_SCROLL,HORIZONTAL_SCROLL,
    }

    public void addMotionEvent(MotionEvent ev){
        mLatestEvent = MotionEvent.obtain(ev);
        mVelocityTracker.addMovement(ev);
    }

    public boolean isXFling() {
        mVelocityTracker.computeCurrentVelocity(1000);
        Log.d("PagesContainer", "velocity is " + mVelocityTracker.getXVelocity());
        return  Math.abs(mVelocityTracker.getXVelocity()) > FLING_GESTURE_THREHOLD;
    }

    public boolean isYFling() {
        mVelocityTracker.computeCurrentVelocity(1000);
        return Math.abs(mVelocityTracker.getXVelocity()) > FLING_GESTURE_THREHOLD;
    }

    public MotionIntent detect(){
        if(null== mStartPoint || null == mLatestEvent){
            return MotionIntent.UNKNOWN;
        }

        final float x1 = mStartPoint.getX();
        final float x2 = mLatestEvent.getX();
        final float dx = Math.abs(x1 - x2);

        final float y1 = mStartPoint.getY();
        final float y2 = mLatestEvent.getY();
        final float dy = Math.abs(y1 - y2);

        if(dx < MAX_MOVEMENT_IN_CLICKING && dy < MAX_MOVEMENT_IN_CLICKING){
            return MotionIntent.UNKNOWN;
        }
        else {
            double v2 = dx;
            v2 *= MOTION_INTENT_FACTOR;
            double v0 = dy;

            if(v2 > v0){
                return MotionIntent.HORIZONTAL_SCROLL;
            }
            else {
                return MotionIntent.VIRTICAL_SCROLL;
            }

        }
    }

    public VelocityTracker getVelocityTracker(){
        return mVelocityTracker;
    }

    public void reset(){
        if(null != mVelocityTracker){
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    public void start(MotionEvent ev){
        if(null != mVelocityTracker){
            reset();
        }
        mStartPoint = MotionEvent.obtain(ev);
        mVelocityTracker = VelocityTracker.obtain();
    }

    public boolean isStarted() {
        return mVelocityTracker != null;
    }

}
